package com.project.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.bean.RoleBean;
import com.project.mapper.IRoleMapper;
import com.project.mapper.IUserMapper;
import com.project.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
@Transactional
public class RoleServiceImpl implements IRoleService {

    @Autowired
    private IRoleMapper mapper;//报错显示，没有写mapper注解
    @Autowired
    private IUserMapper userMapper;

    @Override
    public RoleBean findById(int id) {
        return mapper.findById(id);
    }

    @Override
    public PageInfo<RoleBean> findAll(int page, int pageSize) {
        PageHelper.startPage(page,pageSize);
        PageInfo<RoleBean> pageInfo=PageInfo.of(mapper.findAll());
        return pageInfo;
    }

    @Override
    public void del(int id) {
        mapper.delUserRoleByUserId(id);
        userMapper.delUser(id);
    }

    @Override
    public boolean addRole(RoleBean role, int[] gradeArray) {
        mapper.addRole(role);
        mapper.addRoleByUserId(role.getId(),gradeArray);
        return true;
    }

    @Override
    public void updateRoleGrade(int id, int[] gradeArray) {
        mapper.updateRoleGrade(id, gradeArray);
    }

    @Override
    public List<RoleBean> findRoleGradeByUserId(int userId) {
        return mapper.findRoleGradeByUserId(userId);
    }
}
